/**
 * Copyright (c) 2013-5-3 下午5:04:16 WKM and/or its affiliates. 
 * Use is subject to license terms.
 * All rights reserved.
 */
package org.extblog.wkm.web.dao.upload;

import javax.annotation.Resource;

import org.extblog.wkm.web.dao.AbstractDao;
import org.extblog.wkm.web.util.PropertyUtil;
import org.extblog.wkm.web.vo.FileUploadView;
import org.springframework.jdbc.support.lob.LobHandler;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author WKM
 * Service.java history
 * 1.WKM - 2013-5-3 - Create File
 */
@Controller
public class FileUploadDao extends AbstractDao {
	
	/** Blog字段 **/
	private LobHandler lobHandler;

	public LobHandler getLobHandler() {  
        return lobHandler;  
    }
	
	@Resource
    public void setLobHandler(LobHandler lobHandler) {  
        this.lobHandler = lobHandler;  
    }  
	
	public int insertMusic(MultipartFile fileContent,FileUploadView view){
		log.info("insert:" + PropertyUtil.getValue("file.insert"));
		/*对与clob和blob字段的 入库代码 */
		LobCreatingPreparedStatementCallback lob = new LobCreatingPreparedStatementCallback(lobHandler);
		lob.setFileContent(fileContent);
		lob.setView(view);
		int count = getTemplate().execute(PropertyUtil.getValue("file.insert"),lob);
		
		/*int count = -1;
		Connection conn = null;
		PreparedStatement statement = null;
		try {
			conn = getTemplate().getDataSource().getConnection();
			statement = conn.prepareStatement(PropertyUtil.getValue("file.insert"));
			statement.setString(1, view.getFileId());
			statement.setString(2, fileContent.getOriginalFilename());
			statement.setBytes(3, fileContent.getBytes());
			count = statement.executeUpdate();
			conn.commit();
		} catch (SQLException e){
			e.printStackTrace();
			log.error("SQLException Message:" + e.getMessage());
		} catch (IOException e){
			e.printStackTrace();
			log.error("IOException Message:" + e.getMessage());
		} finally {
			try {
				if(statement != null){
					statement.close();
				}
				if(conn != null){
					conn.close();
				}
			}catch (SQLException e){
				e.printStackTrace();
				log.error("Close SQLException Message:" + e.getMessage());
			}
		}*/
		return count;
	}
}
